/**
 * Copyright &copy; 2016-2022 liuhangjun All rights reserved.
 */
package com.channelsharing.hongqu.portal.api.controller.discovery;

import com.channelsharing.common.entity.Paging;
import com.channelsharing.common.enums.DelFlagEnum;
import com.channelsharing.hongqu.portal.api.controller.BaseController;
import com.channelsharing.hongqu.portal.api.controller.shop.ShopGoodsAddRequestEntity;
import com.channelsharing.hongqu.portal.api.entity.Discovery;
import com.channelsharing.hongqu.portal.api.entity.DiscoveryCategory;
import com.channelsharing.hongqu.portal.api.entity.GoodsDescription;
import com.channelsharing.hongqu.portal.api.entity.ShopGoods;
import com.channelsharing.hongqu.portal.api.service.DiscoveryCategoryService;
import com.channelsharing.hongqu.portal.api.service.DiscoveryService;
import com.channelsharing.hongqu.portal.api.service.GoodsDescriptionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


/**
 * 发现Controller
 *
 * @author liuhangjun
 * @version 2018-06-12
 */
@Api(tags = "发现操作接口")
@Validated
@RestController
@RequestMapping("/v1/discovery")
public class DiscoveryController extends BaseController {

    @Resource
    private DiscoveryService discoveryService;
    @Resource
    private DiscoveryCategoryService discoveryCategoryService;

    @ApiOperation(value = "获取单条介绍内容")
    @GetMapping("/findLast")
    public Discovery findLast() {
        Discovery en=new Discovery();
        Discovery discovery = discoveryService.findOne(en);
        return discovery;
    }

    @ApiOperation(value = "获取单条介绍内容")
    @GetMapping("/{id}")
    public Discovery findOne(@PathVariable Long id) {

        Discovery discovery = discoveryService.findOne(id);
        return discovery;
    }

    @ApiOperation(value = "获取分類列表")
    @GetMapping("/discoveryCategory")
    public List<DiscoveryCategory> discoveryCategory() {
        DiscoveryCategory category = new DiscoveryCategory();
        return discoveryCategoryService.findPaging(category).getRows();
    }

    @ApiOperation(value = "发现信息列表")
    @GetMapping("/all")
    public Paging<Discovery> findPaging(@RequestParam(defaultValue = "0") Integer offset,
                                        @RequestParam(defaultValue = "10") Integer limit, Long categoryId) {
        Discovery entity = new Discovery();
        entity.setCategoryId(categoryId);
        entity.setOffset(offset);
        entity.setLimit(limit);
        entity.setDelFlag(DelFlagEnum.notDelete.getCode());

        return discoveryService.findPaging(entity);

    }

    @ApiOperation(value = "阅读数+1")
    @GetMapping("/modifyReadAmount")
    public void modify(HttpServletRequest request, Long id) {
        Discovery entity = discoveryService.findOne(id);
        entity.setReadAmount(entity.getReadAmount() + 1);
        discoveryService.modify(entity);
    }
}
